package cn.fxl.controller;

import cn.fxl.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@DefaultProperties(defaultFallback = "fallbackGetById")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("getById/{id}")
    // @HystrixCommand(fallbackMethod = "fallbackGetById")
    @HystrixCommand // 标记方法熔断
    public User getById(@PathVariable("id") Long id){
        //String url="http://localhost:1020/getById/"+id;
        String url = "http://user-server/getById/" + id;
       return restTemplate.getForObject(url,User.class);//restTemplate直接调用
    }
  /*  public User fallbackGetById(@PathVariable("id") Long id){
        return new User(-1L,"兜底数据","服务熔断");
    }*/

    public User fallbackGetById(){
        return new User(-1L,"兜底数据","服务熔断");
    }

}
